//
//  HJUpAvatorVC.m
//  project
//
//  Created by yefei on 2019/6/6.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJUpAvatorVC.h"
#import "HJTabBarViewController.h"

@interface HJUpAvatorVC ()
@property (weak, nonatomic) IBOutlet UIImageView *avatorImageView;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIButton *uploadImage;

/**
 头像原图地址
 */
@property (nonatomic,strong) NSString * avatarPic;

/**
 头像地址
 */
@property (nonatomic,strong) NSString * headUrl;
@end

@implementation HJUpAvatorVC

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.avatorImageView rounded:UIRectCornerAllCorners radius:110 borderColor:[UIColor blackColor] borderWidth:1];
    self.nextBtn.layer.cornerRadius = 5;
    self.nextBtn.layer.masksToBounds = YES;
    
    @weakify(self)
    RAC(self.nextBtn,userInteractionEnabled) = [RACSignal combineLatest:@[RACObserve(self.uploadImage, selected)] reduce:^id (NSNumber * selecte){
        @strongify(self)
        if (![selecte boolValue]) {
            self.nextBtn.backgroundColor = [UIColor lightGrayColor];
            return @NO;
        }
        self.nextBtn.backgroundColor = [UIColor blackColor];
        return @YES;
    }];
    
    [[self.uploadImage rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [[TZImagePickerManager manager]selectImageNeedEditFrom:self completion:^(UIImage *origionImg, UIImage *editedImage) {
            self.uploadImage.selected = YES;
            self.avatorImageView.image = editedImage;
            [self uploadHeadImageWithImage:editedImage bgImage:origionImg];
        }];
    }];

    [[self.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        if (self.dic) {
            [parmas addEntriesFromDictionary:self.dic];
        }
        parmas[@"avatarPic"] = self.avatarPic;
        parmas[@"avatar"] = self.headUrl;
        NSString *url = [Api stringByAppendingString:userEditApi];
        [HJHttpTool post:url params:parmas success:^(HJResponeds *result) {
            [HJUserManager setUserModelWithDict:result.data];
            [mUserDefaults setObject:@"1" forKey:@"isLogin"];
            [UIApplication sharedApplication].keyWindow.rootViewController = [[HJTabBarViewController alloc] init];
            [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
        } failure:^(id result) {
            [self showErrorHUDWithError:result];
        }];
    }];
}

// 上传头像
-(void)uploadHeadImageWithImage:(UIImage *)image bgImage:(UIImage *)bgImage {
    NSString * url = [Api stringByAppendingString:UploadImageApi];
    [HJHttpTool uploadImageWithUrl:url params:nil name:@"multipartFiles" imageArray:@[image,bgImage] completion:^(NSArray *result, NSError *error) {
        if (result && result.count >=1) {
            self.headUrl = [NSString stringWithFormat:@"%@",result[0]];
            self.avatarPic = [NSString stringWithFormat:@"%@",result[1]];
        }
    }];
}


@end
